<!--
 * @Description:总账目详情
-->
<template>
  <div class="registered-container multiAccount appMainWarp">

    <el-form :inline="true" :model="searchKey" class="demo-form-inline">

      <el-form-item>
        <el-button :type="0 == quaflat ? 'primary':''" @click="getlisttable(0)">昨天</el-button>
        <el-button :type="1 == quaflat ? 'primary':''" @click="getlisttable(1)">近七天 </el-button>
        <el-button :type="2 == quaflat ? 'primary':''" @click="getlisttable(2)">近1个月</el-button>
      </el-form-item>

      <el-form-item label="交易时间:">
        <el-date-picker
          v-model="searchKey.time"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
          :default-value="[new Date(end_time), new Date(start_time)]"
        />
      </el-form-item>
      <el-form-item label="院区:">
        <el-select v-model="searchKey.hospital_area_no" placeholder="请选择">
          <el-option
            v-for="item in searchKey.Hospital"
            :key="item.no"
            :label="item.name"
            :value="item.no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="平台订单号:">
        <el-input v-model="searchKey.order_no" placeholder="请输入订单号" @change="suborder_no" />
      </el-form-item>

      <el-form-item label="交易类型:">
        <el-select v-model="searchKey.trans_type" placeholder="请选择">
          <el-option
            v-for="item in searchKey.compensationWayList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="选择平台:">
        <el-select v-model="searchKey.system_no" placeholder="请选择">
          <el-option
            v-for="item in searchKey.System"
            :key="item.no"
            :label="item.name"
            :value="item.no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="支付方式:">
        <el-select v-model="searchKey.pay_style" placeholder="请选择">
          <el-option
            v-for="item in searchKey.PayStyle"
            :key="item.no"
            :label="item.name"
            :value="item.no"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="对账业务:">
        <el-select v-model="searchKey.reconciliation" multiple placeholder="请选择" @change="change">
          <el-option
            v-for="item in searchKey.reconciliationList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            :disabled="item.disabled"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="业务类型:">
        <el-cascader
          v-model="searchKey.bill_type"
          :options="searchKey.BillType"
          :props="props"
          change-on-select
          clearable
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="onLoad">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-download" @click="onExport">导出</el-button>
      </el-form-item>

      <!-- <div v-if="conf_key== 'BANK_DATA'" class="tips">
        {{ conf_desc }}
      </div> -->
    </el-form>

    <template>
      <el-table
        ref="table"
        v-loading="page.tableLoading"
        v-el-height-adaptive-table="{bottomOffset: 90}"
        max-height="500px"
        center
        class="table"
        :data="searchDate"
        stripe
        border
      >
        <!-- <el-table-column type="index" width="90" label="序号" /> -->
        <template v-if="system == '100'">
          <el-table-column key="1" label="平台(自助机)交易时间" align="center">
            <template slot-scope="scope">
              <div>
                {{ scope.row.trans_time||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <template v-if="system == '200'">
          <el-table-column key="2" label="平台(线上平台)交易时间" align="center">
            <template slot-scope="scope">
              <div :class="[scope.row.status==3 ? 'abnormal':'normal']">
                {{ scope.row.trans_time||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <template v-if="system == '100'">
          <el-table-column
            v-if="compare == '0' || compare == '1,2' || compare == '2,1' || compare == '1,3' || compare == '3,1' && system == '100'"
            key="3"
            label="平台(自助机)订单号"
            align="center"
          >
            <template slot-scope="scope">
              <div :class="[scope.row.status==3 ? 'abnormal':'normal']">
                {{ scope.row.self_order_no||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <template v-if="system == '200'">
          <el-table-column
            v-if="compare == '0' || compare == '1,2' || compare == '2,1' || compare == '1,3' || compare == '3,1' && system == '100'"
            key="4"
            label="平台(线上平台)订单号"
            align="center"
          >
            <template slot-scope="scope">
              <div :class="[scope.row.status==3 ? 'abnormal':'normal']">
                {{ scope.row.self_order_no||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <el-table-column
          v-if="compare == '0' || compare == '1,2' || compare == '2,1' || compare == '2,3' || compare == '3,2' "
          key="5"
          label="HIS系统订单号"
          align="center"
        >
          <template slot-scope="scope">
            <div :class="[scope.row.status==3 ? 'abnormal':'normal']">
              {{ scope.row.his_order_no||'-' }}
            </div></template>
        </el-table-column>

        <template v-if="system == '100'">
          <el-table-column
            v-if="compare == '0' || compare == '1,2' || compare == '2,1' || compare == '1,3' || compare == '3,1' && system == '100'"
            key="6"
            label="平台(自助机)金额"
            align="center"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.self_pay_fee||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <template v-if="system == '200'">
          <el-table-column
            v-if="compare == '0' || compare == '1,2' || compare == '2,1' || compare == '1,3' || compare == '3,1' && system == '100'"
            key="7"
            label="平台(线上平台)金额"
            align="center"
          >
            <template slot-scope="scope">
              <div>
                {{ scope.row.self_pay_fee||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <el-table-column
          v-if="compare == '0' || compare == '1,2' || compare == '2,1' || compare == '2,3' || compare == '3,2' "
          key="8"
          label="HIS系统金额"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.his_pay_fee||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          v-if="compare == '0' || compare == '1,3' || compare == '3,1' || compare == '2,3' || compare == '3,2'"
          key="9"
          label="支付系统金额"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              {{ scope.row.bank_pay_fee||'-' }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="订单状态" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.status==3 ? 'abnormal':'normal']">
              <el-badge
                is-dot
                class="isDot"
                :type="scope.row.status==3 ? 'danger' : scope.row.status== 2 ? 'primary':'success'"
              />
              {{ scope.row.status == 1 ? '平账' : scope.row.status == 2 ? '退款成功' : scope.row.status == 3 ? '订单异常 ': '-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="异常说明" align="center">
          <template slot-scope="scope">
            <div :class="[scope.row.status==3 ? 'abnormal':'normal']">
              {{ scope.row.desc||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="交易类型" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.trans_type||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="交易院区" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.hospital_area_no||'-' }}
            </div>
          </template>
        </el-table-column>

        <template v-if="system == '100'">
          <el-table-column key="10" label="平台（自助机）" align="center">
            <template slot-scope="scope">
              <div>
                {{ scope.row.system_no||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <template v-if="system == '200'">
          <el-table-column key="11" label="平台（线上平台）" align="center">
            <template slot-scope="scope">
              <div>
                {{ scope.row.system_no||'-' }}
              </div>
            </template>
          </el-table-column>
        </template>

        <el-table-column label="业务类型" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.bill_type_no||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="支付方式" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.pay_style_no||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否已处理" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.is_handle||'-' }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope"><div class="viewdetails" @click="jumpClick(scope.row)">查看详情</div></template>
        </el-table-column>

      </el-table>
    </template>
    <pagination
      :total="page.totalRow"
      :page.sync="page.currentPage"
      :limit.sync="page.pageSize"
      @pagination="onSearch"
    />

  </div>
</template>

<script>
import elHeightAdaptiveTable from '@/directive/el-table'
import { getConfigType } from '@/api/transactRecords/index'
import { someBillDetail } from '@/api/detailed/index'
import Pagination from '@/components/Pagination' // 分页

export default {
  name: 'MultiAccount',
  components: {
    Pagination
  },
  directives: { elHeightAdaptiveTable },

  data() {
    return {
      quaflat: 0, // 按钮默认
      date: 1, // 昨天
      searchKey: {
        order_no: '', // 订单号
        time: '', // 交易日期
        hospital_area_no: '', // 选择院区
        Hospital: [],
        trans_type: '', // 交易类型
        compensationWayList: [
          { id: '', name: '全部' },
          { id: 1, name: '正交易' },
          { id: 2, name: '负交易' }
        ],
        system_no: '100', // 选择平台
        System: [],
        pay_style: '', // 支付方式
        PayStyle: [],
        reconciliation: [2, 3], // 对账业务
        reconciliationList: [
          { id: 0, name: '三方对账', disabled: true },
          { id: 1, name: '平台系统', disabled: true },
          { id: 2, name: 'HIS系统', disabled: false },
          { id: 3, name: '支付系统', disabled: false }
        ],
        bill_type: [], // 业务类型
        BillType: []
      },
      start_time: new Date(this.start_time),
      end_time: new Date(this.end_time),
      props: {
        label: 'name',
        value: 'no',
        children: 'data'
      },

      searchDate: [],
      BankData: {}, // 银行明细配置
      conf_key: '',
      conf_desc: '',
      compare_bus: '',
      comparetrue: true,
      comparefalse: false,

      page: {
        tableLoading: false,
        currentPage: 1, // 当前页
        pageSize: 20, // 默认条数
        totalRow: 0, // 总条数
        totalPage: 0 // 总页数
      },
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      compare: '',
      system: '' // 平台

    }
  },

  created() {
    this.searchKey.time = this.utils.Foryest()
  },
  mounted() {
    this.getConfig()
    this.onLoad()
  },
  methods: {
    // 系统配置搜索
    getConfig(type, val) {
      var that = this
      var params = {}
      getConfigType(params).then(res => {
        // 院区
        var defaultValue = [{ id: '', no: '', name: '全部' }]
        var temp = res.data.Hospital
        var hospitalData = [...defaultValue, ...temp]
        that.searchKey.Hospital = hospitalData
        // 支付方式
        var tempPay = res.data.PayStyle
        var PayStyle = [...defaultValue, ...tempPay]
        that.searchKey.PayStyle = PayStyle
        that.searchKey.System = res.data.System
        that.searchKey.BillType = that.getTreeData(res.data.BillType)
        that.BankData = res.data.BankData
        that.BankData.map((item, key) => {
          that.conf_key = item.conf_key
          that.conf_desc = item.conf_desc
        })
      })
        .catch(() => {
        })
    },
    // 递归判断列表，把最后的children的内容不显示
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].data.length < 1) {
          // children若为空数组，则将children设为undefined
          data[i].data = undefined
        } else {
          // children若不为空数组，则继续 递归调用 本方法
          this.getTreeData(data[i].data)
        }
      }
      return data
    },
    onLoad() {
      this.page.currentPage = 1
      this.onSearch()
    },
    // 获取对账业务
    change(val) {
      const that = this
      var select = []
      var reconciliationList = that.searchKey.reconciliationList
      select = val
      that.searchKey.reconciliation = select
      if (select.length === 0) {
        reconciliationList.forEach(value => {
          value.disabled = false
        })
      } else {
        select.forEach(item => {
          reconciliationList.forEach(value => {
            if (item === 0) {
              if (value.id !== 0) {
                value.disabled = true
              }
            } else if (select.length === 2) {
              if (value.id !== 0) {
                value.disabled = true
              }
            } else if (select.length === 1) {
              if (value.id === 0) {
                value.disabled = true
              }
              if (value.id !== 0) {
                value.disabled = false
              }
            } else {
              if (value.id === 0) {
                value.disabled = true
              }
            }
          })
        })
      }
    },
    onSearch() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.date_type = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.start_time = that.searchKey.time[0]
        params.end_time = that.searchKey.time[1]
      } else {
        params.start_time = ''
        params.end_time = ''
      }
      params.order_no = that.searchKey.order_no
      params.hospital_area_no = that.searchKey.hospital_area_no
      params.trans_type = that.searchKey.trans_type
      params.system_no = that.searchKey.system_no
      params.pay_style = that.searchKey.pay_style
      params.bill_type =
        that.searchKey.bill_type.length >= 1 ? that.searchKey.bill_type[1] : ''

      var reconciliation = that.searchKey.reconciliation
      if (reconciliation.length === 0) {
        that.$message({
          message: '请选择对账业务',
          type: 'warning'
        })
        return false
      } else if (reconciliation.length === 1) {
        reconciliation.forEach(item => {
          if (item === 0) {
            params.compare_bus = that.searchKey.reconciliation.toString()
          } else {
            that.$message({
              message: '请选择不小于两方的对账业务',
              type: 'warning'
            })
            return false
          }
        })
      } else {
        params.compare_bus = that.searchKey.reconciliation.toString()
      }
      that.page.tableLoading = true
      someBillDetail(params)
        .then(res => {
          that.page.tableLoading = false
          that.searchDate = res.data.result
          that.compare = res.data.compare_bus
          that.system = res.data.system_no
          that.page.totalPage = res.data.pageSum
          that.page.totalRow = parseInt(res.data.count)
        })
        .catch(() => {
          that.page.tableLoading = true
        })
    },
    jumpClick(item, compare) {
      this.$router.push({
        path: '/generaLedger/revenueDetail', query: { id: item.id, compare: this.compare }
      })
    },
    // 导出
    onExport() {
      var params = {}
      var that = this
      params.page = that.page.currentPage // 当前页数
      params.limit = that.page.pageSize // 每页展示条数
      params.date_type = that.date
      if (that.searchKey.time && that.searchKey.time.length > 0) {
        params.start_time = that.searchKey.time[0]
        params.end_time = that.searchKey.time[1]
      } else {
        params.start_time = ''
        params.end_time = ''
      }
      params.order_no = that.searchKey.order_no
      params.hospital_area_no = that.searchKey.hospital_area_no
      params.trans_type = that.searchKey.trans_type
      params.system_no = that.searchKey.system_no
      params.pay_style = that.searchKey.pay_style
      params.bill_type =
        that.searchKey.bill_type.length >= 1 ? that.searchKey.bill_type[1] : ''
      var reconciliation = that.searchKey.reconciliation
      if (reconciliation.length === 1) {
        reconciliation.forEach(item => {
          if (item === 0) {
            params.compare_bus = that.searchKey.reconciliation.toString()
          } else {
            that.$message({
              message: '请选择不小于两方的对账方',
              type: 'warning'
            })
          }
        })
      } else {
        params.compare_bus = that.searchKey.reconciliation.toString()
      }
      params.export_flag = 1
      var viewUrl = `/ApiAdmin/Detail/someBillDetail?&page=${params.page}&limit=${params.limit}&date_type=${
        params.date_type
      }&order_no=${params.order_no}&trans_type=${params.trans_type}&hospital_area_no=${
        params.hospital_area_no
      }&start_time=${
        params.start_time
      }&end_time=${params.end_time}&system_no=${params.system_no}&pay_style=${params.pay_style}&bill_type=${params.bill_type}
      &compare_bus=${params.compare_bus}&export_flag=${1}`
      window.location.href = process.env.VUE_APP_BASE_API + viewUrl
    },
    // 验证订单号
    suborder_no() {
      const id = this.searchKey.order_no
      const reg = /^[0-9a-zA-Z]+$/
      if (!reg.test(id)) {
        this.$message({
          type: 'warning',
          message: '请输入正确的订单号'
        })
        this.searchKey.order_no = ''
        return false
      }
    },
    getlisttable(flat) {
      this.quaflat = flat
      if (this.quaflat === 0) {
        this.date = 1
        this.searchKey.time = this.utils.Foryest()

        this.onLoad()
      } else if (this.quaflat === 1) {
        this.date = 2
        this.searchKey.time = this.utils.Forseven()

        this.onLoad()
      } else {
        this.date = 3
        this.searchKey.time = this.utils.Formonth()

        this.onLoad()
      }
    }

  }
}
</script>

<style lang="scss" scoped>
.multiAccount {
  position: relative;
  width: 100%;
  .demo-form-inline {
    .el-button--primary {
      background-color: #579ef8 !important;
      color: #fff !important;
    }
   .el-button {
      background-color: #e8f4ff;
      color: #579ef8;
      border: 1px solid rgba(87, 158, 248, 1);
    }
  }

  .abnormal{
    color: red;
    // text-decoration: underline;
  }
  .normal{
    color:rgb(113, 118, 133)
  }
  .isDot {
    vertical-align: -webkit-baseline-middle;
    color: #1890FF;
  }
  .viewdetails{
    color: #1890FF;
    cursor: pointer;
  }
  .tips{
    color: red;
    font-size: 18px;
    padding: 0 29px 15px;
  }
}

</style>
<style>
.ant-badge-status-success{
  background-color: #4AC74F;
}
</style>

